<template>
  <el-container>
    <el-header>Header <el-button type="info" @click="loginout"> 退出</el-button>
    </el-header>
    <el-container>
      <el-aside width=''>
        <el-menu default-active="2" class="el-menu-vertical-demo"
                 background-color="#545c64" text-color="#fff"
                 active-text-color="#ffd04b" :collapse='isCollapse' router>
          <div class="chgcoll" @click="chgcoll">|||</div>
          <el-submenu :index="item.path" v-for="item in menuList"
                      :key="item.id">
            <template slot="title">
              <!-- 阿里的字体图标库 -->
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item :index="subItem.path" v-for="subItem in item.children"
                          :key="subItem.id">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data: function () {
    return {
      menuList: [],
      isCollapse: false,
      /* 图标和id对应关系的类 */
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      }
    }
  },
  mounted () {
    this.getMenuList()
  },
  methods: {
    loginout () {
      window.sessionStorage.clear()
      this.$router.push('/')
    },
    chgcoll () {
      this.isCollapse = !this.isCollapse
    },
    async getMenuList () {
      const { data: { data, meta } } = await this.$axios.get('menus')
      // console.log(data, meta)
      // 如果出错了,返回空数组
      if (meta.status !== 200) {
        this.$message.error(meta.msg)
      }
      this.menuList = data
    }
  }
}
</script>
<style lang="css" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.chgcoll {
  text-align: center;
  color: #fff;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 150px;
  min-height: 400px;
}
</style>
